package com.qdlc.p2p.dal.util;

import com.qdlc.p2p.common.util.BeanUtil;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.dao.UserCacheDao;
import com.qdlc.p2p.dal.dao.UserDao;
import com.qdlc.p2p.dal.dao.UserIdentifyDao;
import com.qdlc.p2p.dal.dao.UserLoginExtDao;
import com.qdlc.p2p.dal.dao.UserVipDao;
import com.qdlc.p2p.dal.dto.User;
import com.qdlc.p2p.dal.dto.UserCache;
import com.qdlc.p2p.dal.dto.UserIdentify;
import com.qdlc.p2p.dal.dto.UserVip;

/**
 * 用户相关操作
 * @author linya
 */
public class UserUtils {
	
	/**
	 * 获取第三方帐号userCustId
	 * @param userId
	 * @return
	 */
	public static String getUserTppCustId(long userId){
		User user = getUserById(userId);
		return user == null ? "" : user.getTppUserCustId();
	}
	
	/**
	 * 获取第三方帐号绑卡手机号码
	 * @param userId
	 * @return
	 */
	public static String getUserTppMobilePhone(long userId){
		User user = getUserById(userId);
		return user == null ? "" : user.getMobilePhone();
	}
	
	/**
	 * 通过第三方帐号获取用户对象
	 * @param userCustId
	 * @return
	 */
	public static User getUserByTppCustId(String tppCustId){
		UserDao userDao = BeanUtil.getBean(UserDao.class);
		User user = userDao.findByTppCustId(tppCustId);
		return user;
	}
	
	/**
	 * 根据用户ID获取用户对象
	 * @param userId
	 * @return
	 */
	public static User getUserById(final long userId){
		UserDao userDao = BeanUtil.getBean(UserDao.class);
		User user = userDao.getUserById(userId);
		return user;
	}

	/**
	 * 根据用户名称获取用户对象
	 * @param userName
	 * @return
	 */
	public static User getUserByUserName(final String userName){
		UserDao userDao = BeanUtil.getBean(UserDao.class);
		User user = userDao.getUserByUserName(StringUtil.isNull(userName));
		return user;
	}
	
	/**
	 * 获取UserVip信息
	 * @param userId
	 * @return
	 */
	public static UserVip getUserVipByUserId(long userId){
		UserVipDao userVipDao = BeanUtil.getBean(UserVipDao.class);
		UserVip userVip = userVipDao.findByUserId(userId);
		return userVip;
	}
	
	/**
	 * 获取UserCache信息
	 * @param userId
	 * @return
	 */
	public static UserCache getUserCacheByUserId(long userId){
		UserCacheDao userCacheDao = BeanUtil.getBean(UserCacheDao.class);
		UserCache userCache = userCacheDao.findByUserId(userId);
		return userCache;
	}
	
	/**
	 * 获取UserCache信息
	 * @param user
	 * @return
	 */
	public static UserCache getUserCacheByUserId(User user){
		return getUserCacheByUserId(user.getUserId());
	}
	
	/**
	 * 获取UserIdentify
	 * @param userId
	 * @return
	 */
	public static UserIdentify getUserIdentify(long userId){
		UserIdentifyDao userIdentifyDao = BeanUtil.getBean(UserIdentifyDao.class);
		UserIdentify userIdentify = userIdentifyDao.findByUserId(userId);
		return userIdentify;
	}
	
	/**
	 * 获取UserIdentify
	 * @param userId
	 * @return
	 */
	public static UserIdentify getUserIdentify(User user){
		return getUserIdentify(user.getUserId());
	}
	
	/**
	 * 判断用户名、手机号、邮箱是否已经存在
	 * @param username
	 * @return
	 */
	public static boolean exsitUserNameOrPhomeOrEmail(String username){
		UserLoginExtDao userLoginExtDao = BeanUtil.getBean(UserLoginExtDao.class);
		int count = userLoginExtDao.countByUserName(username);
		return count > 0;
	}
}
